<template>
  <div class="login-container">
    <vue-particles
      class="texiao"
      color="#007ACC"
      :particleOpacity="6"
      :particlesNumber="80"
      shapeType="circle"
      :particleSize="4"
      linesColor="#dedede"
      :linesWidth="2"
      :lineLinked="true"
      :lineOpacity="0.4"
      :linesDistance="150"
      :moveSpeed="2"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
    >
    </vue-particles>
    <Nav></Nav>
    
    <div class="tou">
      <img src="../../assets/mao.png" alt="" />
      <h2>与世界分享你的知识、经验和见解</h2>
    </div>

    <div class="login">
      <el-tabs class="tabs" style="text-align:center" value="first">
        <el-tab-pane label="注册" name="first">
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="用户名" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>

            <el-form-item label="手机号码" prop="number">
              <el-input v-model.number="ruleForm.number"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
              <el-input
                type="password"
                v-model="ruleForm.pass"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
              <el-input
                type="password"
                v-model="ruleForm.checkPass"
                autocomplete="off"
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')"
                >注册</el-button
              >
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
            <span class="agreement-tip"
              >点击「注册」按钮，即代表你同意<a href="https://www.xhsd.com/;"
                >《读者书店协议》</a
              ></span
            >
          </el-form>

          <!-- <form method="#" action="javascript:">
        <ul class="group_input">
          <li>
            <input
              type="text"
              placeholder="用户名"
              class="name required"
              id="name"
              v-model="user_name"
            />
           <span class="">{{type.type_name}}</span>
          </li>
          <li>
            <input
              type="text"
              placeholder="手机号(仅支持中国大陆)"
              class="mobile required"
              id="mobile"
              v-model="user_num"
            />
            <span class="">{{type.type_number}}</span>
          </li>
          <li>
            <input
              type="password"
              placeholder="密码(不少于6位)"
              class="psd required"
              id="psd"
              v-model="user_password"
            />
          </li>
        </ul>
        <button class="submit_btn" id="btnSubmit" @click="initBookList">
          注册账号
        </button>
        <span class="agreement-tip"
          >点击「注册」按钮，即代表你同意<a href="https://www.xhsd.com/;"
            >《读者书店协议》</a
          ></span
        >
      </form> -->
        </el-tab-pane>
        <el-tab-pane label="登录" name="second">
          <el-form
            :model="ruleForm"
            status-icon
            
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="用户名" prop="name" >
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>

            <el-form-item label="密码" >
              <el-input
                type="password"
                v-model="ruleForm.pass"
                autocomplete="off"
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-button
                style="width:300px"
                type="primary"
              
                @click="submitForm1('ruleForm')"
                >登录</el-button
              >
            </el-form-item>
          </el-form>
          <div class="states">
            <span class="left"><a href="javascript:;">手机验证码登陆</a></span>
            <span class="right"><a href="javascript:;">无法登陆？</a></span>
          </div>
          <div class="states">
            <a href="javascript:;" class="social_account">社交账号登陆</a>
            
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import axios from "axios";
 import Nav from '../../com/components/Common/Nav';
 import HeadNav from '../../com/components/Common/HeadNav';
export default {
     components:{Nav,HeadNav},
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      } 
      else if(value.length<4||value.length>11) {
         return callback(new Error("用户名必须大于4位和小于11位"));
      }
      
      
      
      else {
        this.initBookList(); //发请求是否注册
        setTimeout(() => {
          if (this.type.type_name == "404") {
            callback(new Error("该用户已被注册"));
          } else {
            callback();
            
          }
        }, 1000);
      }
    };

    var checkNumber = (rule, value, callback) => {
      var regMobile = /^1[3|4|5|7|8][0-9]{9}$/;
      if (!value) {
        return callback(new Error("手机号不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (!regMobile.test(value)) {
            callback(new Error("请输入有效的11位手机号码！"));
          } else {
            callback();
            
          }
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (value.length >12|| value.length<6) {
          callback(new Error("密码位数必须在6和11位之间"));
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
        
      }
    };

    return {
      type: {
        type_name: "hh",
        type_number: "hh",
        type_password: "",
        tab_name:"second"
      },
      ruleForm: {
        pass: "",
        checkPass: "",
        name: "",
        number: "",
        type: 0,
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        number: [{ validator: checkNumber, trigger: "blur" }],
        name: [{ validator: checkName, trigger: "blur" }],
      },
    };
  },

  methods: {
    open(masgs) {
        this.$message(masgs);
      },

    initBookList() {
      this.ruleForm.type=1;
      axios
        .get("http://localhost:8081/fl-user/existUser", {
          params: {
            username: this.ruleForm.name,
          },
        })
        .then((res) => {
         

          console.log(res);
          this.type.type_name = res.data.code;
         
        })
        .catch(function(error) {
        
        })
        .then(function() {
          // always executed
        });
    },

    submitForm(formName) {          //用户注册
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios
            .post("http://localhost:8081/fl-user/registerUser", {
              username: this.ruleForm.name,
              password: this.ruleForm.pass,
            })
            .then((response)=> {
              console.log(response);
              // console.log(this.type.tab_name);
              this.open(response.data.msg);
            })
            .catch(function(error) {
              console.log(error);
            });
        
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
     submitForm1(formName) {            //用户登录
      
      this.$refs[formName].validate((valid) => {
        if (!valid) {
              axios
            .post("http://localhost:8081/fl-user/loginUser", {
              username: this.ruleForm.name,
              password: this.ruleForm.pass,
            })
            .then((res)=> {
              console.log(res);
              if(res.data.code==404){
                console.log(res);
                  this.open(res.data.msg);
                  // alert("用户名密码错误")
              }else{
                this.open(res.data.msg);
             this.$router.push('/index')
              }
            })
            .catch(function(error) {
              console.log(error);
            });
         
          
        } else {
          this.open();
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
}

<style>
.demo-ruleForm {
  position: relative;
  right: 6%;
}
.texiao {
  position: absolute;
  width: 98%;
  height: 98%;
  background: #f7fafc;
}
.login {
  text-align: center;
}
.tou {
  text-align: center;
  width: 500px;
  position: relative;
  left: 37%;
  padding-top: 100px;
}

a {
  text-decoration: none;
}
.tabs {
  position: relative;
  width: 400px;
  left: 40%;
}
h2 {
  margin: 30px 0;
  font-weight: 400;
  font-size: 18px;
}
.group_input {
  position: relative;
  text-align: left;
}
.group_input input {
  width: 274px;
  padding: 16px 12px;
  margin: -1px;
  border: 1px solid #d5d5d5;
  border-radius: 3px;
  background-color: #fff;
}
.group_input li {
  width: 400px;
  position: relative;
  list-style: none;
}
.submit_btn {
  width: 80%;
  box-shadow: none;
  border: 0;
  position: relative;
  left: 30px;
  line-height: 45px;
  font-size: 15px;
  display: block;
  margin: 20px 0;
  color: #fff;
  background-color: #0f88eb;
  border-radius: 3px;
}

.states {
  position: relative;
  width: 100%;
  height: 20px;
  font-size: 90%;
  margin-top: 18px;
  color: #878787;
  overflow: hidden;
}
.states .right a {
  color: #555;
}
.states > .social_account {
  position: absolute;
  left: 0;
  color: #878787;
}

.states > .left {
  float: left;
}
.states > .right {
  float: right;
}
.agreement-tip {
  font-size: 90%;
  color: #878787;
}
</style>
